rm(list=ls())

library(foreign)
library(ggplot2)
library(gridExtra)
library(grid)
library(lattice)

################# 
setwd("")

diffdiffall<-read.csv("study_comparison_w_pooled.csv")
attach(diffdiffall) 

color.names<-c("#1b9e77", "#d95f02", "#7570b3")
axislabels <- c("Same Candidate \n (Our Study 5)", 
                "Same Candidate \n (Nicholson et al. 2016)",  
                "Same Party \n (Our Party Studies Pooled)", 
                "Same Party \n (Our Study 5)",  
                "Same Party \n (Our Study 4)",  
                "Same Party \n (Our Study 3)",  
                "Same Party \n (Our Study 2)", 
                "Same Party \n (Our Study 1)",  
                "Same Ideology \n (Our Study 5)", 
                "Same Ideology \n (Huber & Malhotra 2017)")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_vline(aes(xintercept=2.5), colour="black", linetype="solid", size=0.5) +
  geom_vline(aes(xintercept=8.5), colour="black", linetype="solid", size=0.5) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Attractiveness (in standard deviations)", title="") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text = element_text(size=35)) +  
  geom_segment(aes(x = 2.7, y = 0.3, xend = 3.3, yend = 0.3), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 2.7, y = 0.516, xend = 3.3, yend = 0.516), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 3.3, y = 0.3, xend =3.3 , yend = 0.516), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 2.7, y = 0.3, xend =2.7 , yend = 0.516), linetype="dotted", size=0.8) +
  scale_y_continuous(limits=c(-.1303893, 1.3)) + coord_flip() + 
  scale_colour_manual(values=color.names) + 
  theme(legend.position="none") +
  scale_x_discrete(labels= axislabels)


plot1
ggsave(plot1, file="study_comparison.png", width=10, height=6, scale=2)

### Permutation Distribution
setwd("")

permutation<-read.dta("treatment_permute.dta")

df = data.frame(b_same_party = 5.6, Sepal.Length = 3.9) 

attach(permutation) 

plot2<-ggplot(permutation, x=b_same_party) +
  geom_density(data=permutation, aes(x=b_same_party), size=1.4, colour="#d95f02", fill="#d95f02",  alpha=0.15) + 
  theme_bw() + theme(panel.grid.major = element_blank(), 
                                    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="Effect on Attractiveness (in standard deviations)", y="Density", title="") +
  theme(legend.position="none") +
  scale_x_continuous(limits=c(-0.25, 1.4))  + geom_hline(yintercept=0, colour="white", size=3) + geom_vline(xintercept=0, colour="grey", size=1, linetype="dashed") +
  geom_segment(aes(x = 0.339, y = 3, xend = 0.522, yend = 3), colour = "#d95f02", size=1) +  annotate("point", x = 0.43, y = 3, colour = "#d95f02", size=7,  alpha=1) +
         geom_segment(aes(x = 0.353857143, y = 3, xend = 0.506142857, yend = 3), colour = "#d95f02", size=2) +
  geom_segment(aes(x = 0.809427276, y = 3, xend = 1.297082724, yend = 3), colour = "#1b9e77", size=1) +  annotate("point", x = 1.053255, y = 3, colour = "#1b9e77", size=7,  alpha=1) +
          geom_segment(aes(x = 0.849235884, y = 3, xend = 1.257274116, yend = 3), colour = "#1b9e77", size=2) + 
   geom_segment(aes(x = -0.032848856, y = 3, xend = 0.050710256, yend = 3), colour = "#7570b3", size=1) +  annotate("point", x = 0.0089307, y = 3, colour = "#7570b3", size=7,  alpha=1) +
           geom_segment(aes(x = -0.026027704, y = 3, xend = 	0.043889104, yend = 3), colour = "#7570b3", size=2) + 
     theme(text = element_text(size=20))  + 
  annotate("text", x = 0.43, y = 3.65, label="Pooled Effect", colour = "#d95f02", size=5) +
  annotate("text", x = 0.25, y = 6.75, label="Distribution of \n Permutation Estimates \n (Pooled Effect)", colour = "#d95f02", size=5) + 
annotate("text", x = 1.053255, y = 3.65, label="Nicholson et al. Effect", colour = "#1b9e77", size=5) +
  annotate("text", x = 0.0089307, y = 3.65, label="H&M Effect", colour = "#7570b3", size=5) 
  
plot2
ggsave(plot2, file="permutation_dist.png", width=10, height=5)
	


################# Attractiveness 18-35
setwd("")

diffdiffall<-read.csv("study_comparison_18_35_w_pooled.csv")
attach(diffdiffall) 

color.names<-c("#1b9e77", "#d95f02", "#7570b3")
axislabels <- c("Same Candidate \n (Our Study 5)", 
                "Same Candidate \n (Nicholson et al. 2016)",  
                "Same Party \n (Our Party Studies Pooled)", 
                "Same Party \n (Our Study 5)",  
                "Same Party \n (Our Study 4)",  
                "Same Party \n (Our Study 3)",  
                "Same Party \n (Our Study 2)", 
                "Same Party \n (Our Study 1)",  
                "Same Ideology \n (Our Study 5)", 
                "Same Ideology \n (Huber & Malhotra 2017)")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Attractiveness (in standard deviations)", title="") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text = element_text(size=35)) +  
  geom_segment(aes(x = 2.7, y = 0.28, xend = 3.3, yend = 0.28), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 2.7, y = 0.552, xend = 3.3, yend = 0.552), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 3.3, y = 0.28, xend =3.3 , yend = 0.552), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 2.7, y = 0.28, xend =2.7 , yend = 0.552), linetype="dotted", size=0.8) +
  
  scale_y_continuous(limits=c(-.1303893, 1.9)) + coord_flip() + scale_colour_manual(values=color.names) + theme(legend.position="none") +
  scale_x_discrete(labels= axislabels)


plot1
ggsave(plot1, file="study_comparison_18_35.png", width=10, height=6, scale=2)


################# Scaled Outcomes
setwd("/")

diffdiffall<-read.csv("study_comparison_factor_weight_scales_meta_analysis.csv")
attach(diffdiffall) 

axislabels <- c("Same Party \n (Our Studies Pooled)", "Same Party \n (Our Study 5)", "Same Party \n (Our Study 4)", "Same Party \n (Our Study 3)", "Same Party \n (Our Study 2)", "Same Party \n (Our Study 1)",  "Same Ideology \n (Huber & Malhotra 2017)")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, colour="#d95f02", size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se), colour="#d95f02", width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se), colour="#d95f02", width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Attractiveness (in standard deviations)", title="") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text = element_text(size=35)) +  
  geom_segment(aes(x = 0.9, y = 0.4, xend = 1.3, yend = 0.4), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 0.9, y = 0.6, xend = 1.3, yend = 0.6), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 1.3, y = 0.4, xend =1.3 , yend = 0.6), linetype="dotted", size=0.8) +
  geom_segment(aes(x = 0.9, y = 0.6, xend =0.9 , yend = 0.6), linetype="dotted", size=0.8) +
  
  scale_y_continuous(limits=c(-.1, 1)) + coord_flip() + theme(legend.position="none") +
  scale_x_discrete(labels= axislabels)


plot1
ggsave(plot1, file="study_comparison_factor_weight_scales.png", width=10, height=6, scale=2)


################# Messaging (Us and H/M)
setwd("")

diffdiffall<-read.csv("study_comparison_message_pooled.csv")
attach(diffdiffall) 

color.names<-c("#1b9e77", "#d95f02", "#7570b3")
axislabels <- c("Same Candidate \n (Our Study 5)",
                "Same Party \n (Our Party Studies Pooled)", 
                "Same Party \n (Our Study 5)", 
                "Same Party \n (Our Study 4)", 
                "Same Party \n (Our Study 3)", 
                "Same Party \n (Our Study 2)", 
                "Same Party \n (Our Study 1)",  
                "Same Ideology \n (Our Study 5)",
                "Same Ideology \n (Huber & Malhotra 2017)")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Messaging (in standard deviations)", title="") +
  theme(text = element_text(size=35)) + 
  scale_y_continuous(limits=c(-0.05, 1.3)) + coord_flip() + scale_colour_manual(values=color.names) + theme(legend.position="none") +
  scale_x_discrete(labels= axislabels)


plot1
ggsave(plot1, file="study_comparison_message.png", width=10, height=6, scale=2)

################# LT Dating (Us and H/M)
setwd("")

diffdiffall<-read.csv("study_comparison_lt_dating_w_pooled.csv")
attach(diffdiffall) 

color.names<-c("#1b9e77", "#d95f02", "#7570b3")
axislabels <- c("Same Candidate \n (Our Study 5)",
                "Same Party \n (Our Party Studies Pooled)", 
                "Same Party \n (Our Study 5)", 
                "Same Party \n (Our Study 4)", 
                "Same Party \n (Our Study 3)", 
                "Same Party \n (Our Study 2)", 
                "Same Party \n (Our Study 1)",  
                "Same Ideology \n (Our Study 5)",
                "Same Ideology \n (Huber & Malhotra 2017)")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Relationship (in standard deviations)", title="") +
  theme(text = element_text(size=35)) + 
  scale_y_continuous(limits=c(-0.05, 1.3)) + coord_flip() + scale_colour_manual(values=color.names) + theme(legend.position="none") +
  scale_x_discrete(labels= axislabels)


plot1
ggsave(plot1, file="study_comparison_lt_dating.png", width=10, height=6, scale=2)

################# Study 4
rm(list=ls())

setwd("")

diffdiffall<-read.csv("study_4.csv")
attach(diffdiffall) 

color.names<-c( "#d95f02")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Relationship Outcomes (in standard deviations)", title="") +
  theme(text = element_text(size=35)) + 
  scale_y_continuous(limits=c(-0.05, 1.3)) + coord_flip() + scale_colour_manual(values=color.names) + theme(legend.position="none") 

plot1
ggsave(plot1, file="study_4.png", width=10, height=6, scale=2)


################# Study 5
rm(list=ls())

setwd("")

diffdiffall<-read.csv("study_5.csv")
attach(diffdiffall) 

color.names<-c("#1b9e77", "#d95f02", "#7570b3")

plot1<-ggplot(diffdiffall, aes(y=late,  x=reorder(variable_label, num)))+ 
  geom_hline(aes(yintercept=0), colour="black", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, aes(colour=factor(color)), size=12) +
  geom_errorbar(aes(ymin=late-1.96*se, ymax=late+1.96*se, colour=factor(color)), width=0, size=2) +
  geom_errorbar(aes(ymin=late-1.64*se, ymax=late+1.64*se, colour=factor(color)), width=0, size=4) +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  labs(x="", y="Effect on Relationship Outcomes (in standard deviations)", title="") +
  theme(text = element_text(size=35)) + 
  scale_y_continuous(limits=c(-0.05, 1.3)) + coord_flip() + 
  scale_colour_manual(values=color.names) + theme(legend.position="none") 

plot1
ggsave(plot1, file="study_5.png", width=10, height=6, scale=2)

######## COVARIATE BALANCE ACROSS STUDIES
rm(list=ls())

setwd("")

all3<-read.dta("balance_experiments_1_5.dta")

attach(all3) 

pd <- position_dodge(width=0.62)  

color.names <- c("#8c510a", "#d8b365", "#f6e8c3", "#c7eae5", "#5ab4ac", "#01665e")

plot1<-ggplot(all3, aes(y=coef,  x=outcome))+ 
  geom_hline(aes(yintercept=0.36), colour="grey8", linetype="dashed", size=2) +
  geom_hline(aes(yintercept=-0.36), colour="grey8", linetype="dashed", size=2) +  
  geom_hline(aes(yintercept=0), colour="#990000", linetype="dashed", size=2) +
  geom_point(data=all3, size=9,  aes(colour = factor(experiment)), position = pd) +
  geom_errorbar(data=all3, aes(ymin=coef-1.96*stderr, ymax= coef+1.96*stderr, colour = factor(experiment)), position = pd, width=0, size=3) +
  scale_colour_manual(values=color.names) + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                               panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.position="bottom", legend.direction="horizontal") +
  labs(x="", y="Effect on Pre-Treatment Variables (std.)", title="") +
  theme(text = element_text(size=30)) + 
  theme(axis.text.x = element_text(angle=90, hjust = 1)) +
  labs(color='Experiment #') 
plot1 

ggsave(plot1, file="covariate_balance_experiments_1_5_pooled.png", width=10, height=6, scale=2)


######## HETs ACROSS STUDIES
rm(list=ls())

setwd("")

all3<-read.dta("hets_studies_1_5_pooled_cleaned.dta")

attach(all3) 

plot1<-ggplot(all3, aes(y=coef,  x=outcome))+ 
  geom_hline(aes(yintercept=0.36), colour="grey8", linetype="dashed", size=2) +
  geom_hline(aes(yintercept=-0.36), colour="grey8", linetype="dashed", size=2) +  
  geom_hline(aes(yintercept=0), colour="#990000", linetype="dashed", size=2) +
  geom_point(data=all3, size=9, colour="black") +
  geom_errorbar(data=all3, aes(ymin=coef-1.96*stderr, ymax= coef+1.96*stderr), colour="black", width=0, size=3) +
  facet_wrap(~var, nrow=2)  + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                               panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.position="bottom", legend.direction="horizontal") +
  labs(x="Outcome Variables", y="Effect on Outcomes (std.)", title="") +
  theme(text = element_text(size=22)) + 
  theme(axis.text.x = element_text(angle=90, hjust = 1)) + 
  scale_y_continuous(limits=c(-0.7, 0.7))
plot1 

ggsave(plot1, file="hets_1_5_pooled.png", width=11, height=6, scale=2)




